﻿using System;
using System.IO;

namespace PS.Framework
{
    public class FNHelper
    {

        public static string GetDateTimeToHourString()
        {
            string day = DateTime.Now.Day.ToString();
            if (day.Length < 2) day = "0" + day;
            string month = DateTime.Now.Month.ToString();
            if (month.Length < 2) month = "0" + month;
            string hour = DateTime.Now.Hour.ToString();
            if (hour.Length < 2) hour = "0" + hour;

            return
           DateTime.Now.Year.ToString() +month + day + hour ; 

        }
        public static string GetDateTimeJustMinutesString()
        {

            string day = DateTime.Now.Day.ToString();
            if (day.Length < 2) day = "0" + day;
            string month = DateTime.Now.Month.ToString();
            if (month.Length < 2) month = "0" + month;
            string hour = DateTime.Now.Hour.ToString();
            if (hour.Length < 2) hour = "0" + hour;
            string min = DateTime.Now.Minute.ToString();
            if (min.Length < 2) min = "0" + min;

            return
           DateTime.Now.Year.ToString() + month + day + hour + min;

        }

        public static string GetSpecificDateTimeJustMinutesString(DateTime dt)
        {

            string day = dt.Day.ToString();
            if (day.Length < 2) day = "0" + day;
            string month = dt.Month.ToString();
            if (month.Length < 2) month = "0" + month;
            string hour = dt.Hour.ToString();
            if (hour.Length < 2) hour = "0" + hour;
            string min = dt.Minute.ToString();
            if (min.Length < 2) min = "0" + min;

            return
           dt.Year.ToString() + month + day + hour + min;

        }

        public static string GetDateTimeString()
        {
            string day = DateTime.Now.Day.ToString();
            if (day.Length < 2) day = "0" + day;
            string month = DateTime.Now.Month.ToString();
            if (month.Length < 2) month = "0" + month;
            string hour = DateTime.Now.Hour.ToString();
            if (hour.Length < 2) hour = "0" + hour;
            string min = DateTime.Now.Minute.ToString();
            if (min.Length < 2) min = "0" + min;
            string sec = DateTime.Now.Second.ToString();
            if (sec.Length < 2) sec = "0" + sec;

            return
           DateTime.Now.Year.ToString() + month + day + hour + min +sec;
            

        }

        public static string GetFNPatternSuffixDTGuid(
            string suffix, string ext)
        {
            try
            {
                ext = ext.Replace(".","");
            }
            catch (Exception)
            {
                
            }

            string separator = "_";
            return  suffix + separator +
                  FNHelper.GetDateTimeString() + separator
                + Guid.NewGuid().ToString() + ext;

        }

        public static string GetFNPatternSuffixDTGuid(
            string suffix, string ext,string separator)
        {
            try
            {
                ext = ext.Replace(".", "");
            }
            catch (Exception)
            {
                
            }

            return  suffix + separator +
                  FNHelper.GetDateTimeString() + separator
                + Guid.NewGuid().ToString() + "." + ext;

        }
        public static string GetFNPatternSuffixDTGuid(string targetpath,
            string suffix, string ext,string separator)
        {
            try
            {
                ext = ext.Replace(".", "");
            }
            catch (Exception)
            {
                
            }
            string filename =
 suffix + separator +
                  FNHelper.GetDateTimeString() + separator
                + Guid.NewGuid().ToString().Replace("-","") + "." + ext;

            return Path.Combine( targetpath,filename)
                ;

        }
        /// <summary>
        /// handle to remove some character to create clean filename.
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="replaceChar"></param>
        /// <returns></returns>
        public static string CreateCleanFileName(string sourceString, string replaceChar)
        {
            if (replaceChar == string.Empty) replaceChar = "";
            string r = sourceString;
            try
            {
                string[] toreplace = { "-", "/", "\\", " ", ";", "," };
                foreach (string to in toreplace)
                {
                    replacer(to, replaceChar, ref r); 
                }
                
            }
            catch (Exception)
            {
                return string.Empty;
                throw new Exception("string replacement failed...");
            }
            return r;
        }

        /// <summary>
        /// replace a char if not the same
        /// </summary><remarks>I feel vege and that this is a useless function !!! anyway</remarks>
        /// <param name="srcArepl"></param>
        /// <param name="by"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        private static string replacer(string srcArepl,string by,ref string r)
        {
            //the caracter is not the same so we  replace it !
            if (srcArepl != by) r = r.Replace(srcArepl, by);
            return r; 
        }
    }
}
